package oop.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oop.data.TopicInfo;
import oop.db.Database;

public class TopicInfoDAO {

	public static List<TopicInfo> fetchByNameIgnoreCase(String name) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT top_id, top_name, ques_count "
					+ "FROM vwTopicInfo " 
					+ "WHERE top_name LIKE ? "
					+ "LIMIT 0, 50";
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, name);
			rs = stmt.executeQuery();
			
			List<TopicInfo> topicInfos = new ArrayList<TopicInfo>();
			while (rs.next()) {
				topicInfos.add(new TopicInfo(rs.getLong(1), rs
						.getString(2), rs.getInt(3)));
			}
			return topicInfos;
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

}
